﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using HYSoft.BLL;

namespace HYSoft.Web
{
    public partial class equipment_add55 : System.Web.UI.Page
    {
        ListItem item;
        string adminnoss = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            adminnoss=Request.QueryString["adminnos"];
            if (Context.Session["uid"] == null || Context.Session["uid"] == "")
            {
                Context.Response.Write("<script language=\"javascript\" type=\"text/javascript\">");
                Context.Response.Write("alert('对不起,你登录时间超时,请重新登录');top.location.href=\"index.html\";");
                Context.Response.Write("</script>");
                Context.Response.End();
            }
            string units1 = "";
            if (!IsPostBack)
            {
                DataTable dt = new usr2monitor().GetList("[user_index]=" + Session["uid"]).Tables[0];
                foreach (DataRow row in dt.Rows)
                {
                    item = new ListItem();
                    item.Value = row["index"].ToString();
                    item.Text = row["addr"].ToString();
                    jcd.Items.Add(item);
                }
                dt.Clear();
                dt.Dispose();
                //传感器的绑定
               string  sql = " [index]!=0";
                DataTable dt1 = new BLL.sensor_node().GetList(sql).Tables[0];
                if (dt1.Rows.Count > 0)
                {
                    cgqlx.DataSource = dt1.DefaultView;
                    cgqlx.DataTextField = "name";
                    cgqlx.DataValueField = "adminno";
                    cgqlx.DataBind();

                    cgqlx.Items.FindByValue(adminnoss).Selected = true;
                    dt1.Clear();
                    dt1.Dispose();
                }
                DataTable dt3 = new user().GetList("[index]=" + Session["uid"]).Tables[0];
                units1 = dt3.Rows[0]["unit"].ToString();
                dt3.Clear();
                dt3.Dispose();
                DataTable dt2 = new user().GetList("[unit]='" + units1 + "' and [type]=0 and [intvalue2]=1 ").Tables[0];
                foreach (DataRow row in dt2.Rows)
                {
                    item = new ListItem();
                    item.Value = row["index"].ToString();
                    item.Text = row["name"].ToString();
                    jcy.Items.Add(item);
                }
                dt2.Clear();
                dt2.Dispose();
                mac_list();
            }
        }

        private void mac_list()
        {
            DataTable dt2 = new mac_value().GetList("[unit_index]=" + Session["uid"] + " and [level2]=0 and [adminno]= " + this.cgqlx.Value).Tables[0];
            foreach (DataRow row in dt2.Rows)
            {
                item = new ListItem();
                item.Value = row["mac"].ToString();
                item.Text = row["mac"].ToString();
                mac.Items.Add(item);
            }
            dt2.Clear();
            dt2.Dispose();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string fileName = FileUpload1.FileName.ToString();
            string filePath = string.Empty;
            if (fileName.Trim() == string.Empty)
            {
                Response.Write("<script>alert('安装图片未选择！');</script>");
                return;
            }
            else
            {
                fileName = Guid.NewGuid().ToString("N") + ".jpg";
                filePath = "files/" + fileName;
            }
            string filetype = fileName.Substring(fileName.LastIndexOf(".") + 1);
            filetype = filetype.ToLower();
            if (filetype == "jpg" || filetype == "gif" || filetype == "png")
            {
                FileUpload1.SaveAs(Server.MapPath("files/") + fileName);
                Model.gprsNode gprsNodemode = new Model.gprsNode();
                gprsNodemode.adminno = 55;
                gprsNodemode.tux = filePath;
                gprsNodemode.areano = Convert.ToInt32(this.jcd.Value);
                gprsNodemode.mac = Convert.ToInt32(this.mac.Value);
                gprsNodemode.simno = this.TextBox16.Value.TrimEnd();
                gprsNodemode.addr = this.TextBox17.Value.TrimEnd();
                gprsNodemode.gongdian = this.TextBox4.Value.TrimEnd();
                gprsNodemode.charvalue1 = this.jcd.Value;
                gprsNodemode.value0 = Convert.ToInt32(this.ddlYLJ.SelectedValue);
                gprsNodemode.value1 = Convert.ToInt32(this.ddlYYLX.SelectedValue);
                int result = new gprsNode().Add(gprsNodemode);
                if (result > 0)
                {
                    //charvalue1:坡面编号
                    //value0:是否有雨量计 0：否；1：是
                    //value1:基站应用类型 0：地表变形；1：深部位移
                    Model.node_to_user node_to_usermodel = new Model.node_to_user();
                    node_to_usermodel.user_index = Convert.ToInt32(this.jcy.Value);
                    node_to_usermodel.mac = Convert.ToInt32(this.mac.Value);
                    new node_to_user().Add(node_to_usermodel);
                    Model.mac_value mac_valuemodel = new mac_value().GetModel(Convert.ToInt32(this.mac.Value));
                    mac_valuemodel.level2 = 1;
                    new mac_value().Update(mac_valuemodel);
                    Response.Write("<script>alert('传感器节点添加成功！请继续添加。');window.location = 'equipment_add"+adminnoss+".aspx?adminnos="+adminnoss+"';</script>");
                }
            }
        }
    }
}